package com.atguigu.gmall.product.contioller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import com.sun.xml.internal.bind.v2.runtime.reflect.ListTransducedAccessorImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;

/**
 * description:
 * create by sxy 2021/5/8
 * <p>
 * 后台管理中心
 */
@RestController
@RequestMapping("/admin/product")
//@CrossOrigin  配置了网关的配置文件之后这个注解必须取消
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @GetMapping("/getCategory1")
    public Result getCategory1() {

        List<BaseCategory1> baseCategory1List = managerService.getCategory1();

        return Result.ok(baseCategory1List);
    }

    //根据一级id查询二级分类
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable long category1Id) {

        List<BaseCategory2> baseCategory2List = managerService.getCategory2(category1Id);

        return Result.ok(baseCategory2List);
    }

    //根据一级id查询二级分类
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable long category2Id) {

        List<BaseCategory3> baseCategory3List = managerService.getCategory3(category2Id);

        return Result.ok(baseCategory3List);
    }

    //根据一二三级分类的id  层级查询平套属性级属性值表
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable long category1Id,
            @PathVariable long category2Id,
            @PathVariable long category3Id
    ) {

        List<BaseAttrInfo> baseAttrInfoList = managerService.
                attrInfoList(category1Id, category2Id, category3Id);

        return Result.ok(baseAttrInfoList);
    }

    //添加平台属性
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        managerService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    //修改平台属性
    //1根据attrId 查询平台属性对象

    @GetMapping("getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(
            @PathVariable("attrId") Long attrId) {
        BaseAttrInfo baseAttrInfo = managerService.getAttrInfo(attrId);
        List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();
        return Result.ok(baseAttrValueList);

    }

    //查询品牌列表
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(
            @PathVariable Integer page,
            @PathVariable Integer limit) {

        IPage iPage = managerService.baseTrademark(page, limit);
        return Result.ok(iPage);
    }

    //查询spu商品列表 分页
    @GetMapping("{page}/{limit}")
    public Result spuInfoList(@PathVariable Integer page, @PathVariable Integer limit,
                              Long category3Id) {
        IPage iPage = managerService.spuInfoList(page, limit, category3Id);
        return Result.ok(iPage);
    }

    //查询品牌列表
    @GetMapping("baseTrademark/getTrademarkList")
    public Result getTrademarkList() {
        List<BaseTrademark> trademarkList = managerService.getTrademarkList();
        return Result.ok(trademarkList);
    }

    //查询销售属性
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList() {
        List<BaseSaleAttr> baseSaleAttrList = managerService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //添加spu
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        managerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //根据商品id  spuid 查询所有图片 显示在sku添加页面，为了叫管理员来选择
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId) {
        List<SpuImage> spuImageList = managerService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //根据商品id spuid查找销售属性 及值表
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList = managerService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);

    }

    //保存sku
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //分页查询sku列表
    @GetMapping("list/{page}/{limit}")
    public Result skuListPage(@PathVariable Integer page,
                              @PathVariable Integer limit) {
        IPage iPage = managerService.skuListPage(page, limit);
        return Result.ok(iPage);

    }
    //上架
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {
        //修改db中的字段 is_sale
        managerService.onSale(skuId);
        return Result.ok();
    }
    //下架
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId) {
        managerService.cancelSale(skuId);
        return Result.ok();
    }
}
